package com.shut.vehicle.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ValidationException;
import java.io.IOException;
import java.util.*;

/**
 * @author 卢斌
 */
@Slf4j
public class RailwayUtil {


    /**
     * 构造层级码
     *
     * @param ids   各级ID
     * @return  层级码
     */
    public static String buildLevelCode(Integer... ids) {
        Object[] params = ids.clone();
        if (params == null || params.length == 0) {
            return StringUtils.EMPTY;
        }
        StringBuilder buffer = new StringBuilder();
        for (Object param : params) {
            Integer eachId = (Integer) param;
            buffer.append(StrValue.LEFT).append(eachId).append(StrValue.RIGHT);
        }
        return buffer.toString();
    }


    /**
     * 构造层级码
     *
     */
    public static String buildLevelCode(String parentLevelCode, Integer id) {
        String code = StrValue.LEFT + id.toString() + StrValue.RIGHT;
        if (StringUtils.isBlank(parentLevelCode)) {
            return code;
        }
        return parentLevelCode + code;
    }


    /**
     * 层级码解析成部门ID
     */
    public static List<Integer> parentIdSet(String levelCode) {
        if (StringUtils.isBlank(levelCode)) {
            return new ArrayList<>();
        }
        levelCode = replaceAll(levelCode,StrValue.RIGHT+StrValue.LEFT.toString(), StrValue.STUB.toString());
        levelCode = replaceAll(levelCode, StrValue.RIGHT.toString(), StrValue.EMPTY);
        levelCode = replaceAll(levelCode, StrValue.LEFT.toString(), StrValue.EMPTY);
        String[] levelIdStr = levelCode.split(StrValue.STUB.toString());
        List<Integer> parentSet = new LinkedList<>();
        for (String eachLevel : levelIdStr) {
            if (StringUtils.isBlank(eachLevel)) {
                continue;
            }
            parentSet.add(Integer.parseInt(eachLevel));
        }
        return parentSet;
    }


    /**
     * 容器参数修改成逗号分隔的参数字符串
     */
    public static <T> String translateCollectionParam(Collection<T> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return StringUtils.EMPTY;
        }
        StringBuilder result = new StringBuilder();
        for (T each : collection) {
            result.append(",").append(each.toString());
        }
        result.deleteCharAt(0);
        return result.toString();
    }


    /**
     * 二维集合压缩成一维
     */
    public static <T> Set<T> joinSet(Collection<Collection<T>> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return new HashSet<T>();
        }
        Set<T> resultSet = new HashSet<T>();
        for (Collection<T> eachSet : collection) {
            resultSet.addAll(eachSet);
        }
        return resultSet;
    }


    /**
     * 批量从集合中获取值
     */
    public static <K,V> List<V> batchGetMap(Map<K, V> kvMap, Collection<K> param) {
        if (kvMap == null || kvMap.keySet().isEmpty() || CollectionUtils.isEmpty(param)) {
            return new ArrayList<>();
        }
        List<V> result = new LinkedList<>();
        for (K eachKey : param) {
            V oneV = kvMap.get(eachKey);
            result.add(oneV);
        }
        return result;
    }


    public static boolean levelIsContent(String levelCode, Set<String> levelRange) {
        if (levelRange.isEmpty()) {
            return false;
        }
        for (String eachLevel : levelRange) {
            if (eachLevel.startsWith(levelCode)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 不需要正则表达式匹配的replaceAll
     */
    public static String replaceAll(String model, String from, String target) {
        return org.springframework.util.StringUtils.replace(model, from, target);
    }


}
